package m3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //建立连接
        ConnectionFactory f = new   ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c= f.newConnection().createChannel();


        //定义队列 定义交换机
        String queue = UUID.randomUUID().toString().replace("-","");//随机队列名
        c.queueDeclare(queue,false,true,true,null);
        c.exchangeDeclare("logs","fanout");

        //绑定交换机
        c.queueBind(queue,"logs","");

        //自己的队列需要取名的 每个队列都有自己的名字
        //让队列有自己的随机的名字
        //队列可以共享  使用群发模式

        //定义回调
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String s  = new String(message.getBody());
                System.out.println("收到："+s);
            }
        };
       CancelCallback cancelCallback =  new CancelCallback() {
           @Override
           public void handle(String consumerTag) throws IOException {

           }
       };
        //消费数据
        c.basicConsume(queue,true, deliverCallback, cancelCallback);
    }

}
